//
//  YHNetworkManager.m
//  YHBaseProject
//
//  Created by yuhang liu on 2022/11/18.
//  Copyright © 2022 lyh. All rights reserved.
//

#import "YHNetworkManager.h"

static CGFloat const kOvertime = 20.0;


@implementation YHNetworkManager

+ (YHNetworkManager *)sharedInstance {
    static YHNetworkManager *networkManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        networkManager = [[self alloc] init];
    });
    return networkManager;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _sessionManager = [[AFHTTPSessionManager alloc] init];
        /*
         requestSerializer它是AFNetworking参数编码的序列化器：
         AFHTTPRequestSerializer 普通的http的编码格式，二进制
         AFJSONRequestSerializer json编码格式
         AFPropertyListRequestSerializer plist 编码格式
         
         responseSerializer它是AFNetworking返回内容编码的序列化器：
         AFHTTPResponseSerializer 普通的http的编码格式，二进制
         AFJSONResponseSerializer json编码格式
         AFXMLParserRequestSerializer plist编码格式
         AFXMLDocumentResponseSerializer XML,只能返回XMLParser，还需要自己解析
         AFPropertyListRequestSerializer plist编码格式
         AFImageResponseSerializer image
         AFCompoundRequestSerializer 组合
         
         */
        _sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
        _sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
        [_sessionManager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
        _sessionManager.requestSerializer.timeoutInterval = kOvertime;
        [_sessionManager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
        _sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/xml", @"text/plain", @"image/gif", nil];
        // 是否允许无效证书, 默认为NO
        _sessionManager.securityPolicy.allowInvalidCertificates = YES;
        // 是否校验域名, 默认为YES
        _sessionManager.securityPolicy.validatesDomainName = NO;
        // 缓存 有缓存就用缓存，没有缓存就重新请求
        //        _sessionManager.requestSerializer.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
    }
    return self;
}

- (BOOL)isReachable {
    return [AFNetworkReachabilityManager sharedManager].reachable;
}

- (NSMutableDictionary *)logUrlDic {
    if (_logUrlDic == nil) {
        _logUrlDic = [NSMutableDictionary dictionary];
    }
    return _logUrlDic;
}

@end
